package com.sx.micowell.module.train.plan

import android.os.Build
import android.view.View
import androidx.annotation.RequiresApi
import androidx.recyclerview.widget.LinearLayoutManager
import com.alibaba.android.arouter.facade.annotation.Autowired
import com.alibaba.android.arouter.facade.annotation.Route
import com.example.mvvm.base.data.DataBindingConfig
import com.miloyu.modelnet.response.TrainListVO
import com.miloyu.modelnet.response.log
import com.sx.micowell.BR
import com.sx.micowell.R
import com.sx.micowell.databinding.FragTrainStepPlanBinding
import com.sx.micowell.framework.app.IntentKey
import com.sx.micowell.framework.app.MyFragment
import com.sx.micowell.framework.arouth.RoutePath
import com.sx.micowell.framework.model.PlanModel
import com.sx.micowell.framework.model.TrainModel
import com.sx.micowell.module.MainActivity
import com.sx.micowell.module.trainplan.TrainPlanListAdapter
import java.util.*

/**
 * @Description 训练计划
 * @Author 梁浩文
 * @Date 2021/8/30 15:52
 */

@Route(path = RoutePath.TRAIN_STEP_PLAN)
class TrainStepPlanFragment :MyFragment<FragTrainStepPlanBinding, MainActivity>(){

    @Autowired(name = IntentKey.ID)
    @JvmField
    var  Id: String =""
    private lateinit var lisenter: (MutableList<TrainListVO>) -> Unit?;
    private var trainPlanListAdapter : TrainPlanListAdapter? = null
    private val viewModel: PlanModel by lazy {
        getFragmentScopeViewModel(PlanModel::class.java)
    }

    fun setOnLabelCheckedListener(e: (MutableList<TrainListVO>) -> Unit) {
        this.lisenter = e
    }


    override fun initViewObservable() {
        super.initViewObservable()
        setTrainPlanList()
    }
    override fun getDataBindingConfig(): DataBindingConfig =
        DataBindingConfig(R.layout.frag_train_step_plan, BR.viewModel, viewModel)

    override fun initView() {

    }

    @RequiresApi(Build.VERSION_CODES.O)
    override fun initData() {
        val calendar = Calendar.getInstance()
        viewModel.dayPlanList("${calendar.get(Calendar.YEAR)}/${calendar.get(Calendar.MONTH)+1}/${calendar.get(Calendar.DAY_OF_MONTH)}",Id)
    }

    private fun setTrainPlanList() {
        //取消recyclerview单独的滑动效果
        binding.recyclerTrainPlan.isNestedScrollingEnabled = false
        //设置布局管理器
        binding.recyclerTrainPlan.layoutManager = LinearLayoutManager(context)

        viewModel.trainListEvent.observe(this, {
            if (it.size > 0) {
                binding.layoutEmpty.visibility = View.GONE
                if (trainPlanListAdapter == null) {
                    viewModel.planSelectData.clear()
                    trainPlanListAdapter = TrainPlanListAdapter(viewModel.planSelectData)
                    trainPlanListAdapter?.showDeleteView=false
                    trainPlanListAdapter?.data?.clear()
                    trainPlanListAdapter?.data = it
                    binding.recyclerTrainPlan.adapter = trainPlanListAdapter

//                    trainPlanListAdapter?.addChildClickViewIds(R.id.item_plan_layout)
//                    trainPlanListAdapter?.setOnItemChildClickListener { adapter, view, position ->
//                        val trainBean: TrainListVO = adapter.getItem(position) as TrainListVO
//
//                        if(viewModel.planSelectData.contains(trainBean)){
//                            viewModel.planSelectData.clear()
//                        }else{
//                            viewModel.planSelectData.clear()
//                            viewModel.planSelectData.add(trainBean)
//                        }
//                        lisenter?.invoke(viewModel.planSelectData)
//                        trainPlanListAdapter?.notifyDataSetChanged()
//                    }
                } else {
                    trainPlanListAdapter?.data = it
                    binding.recyclerTrainPlan.adapter = trainPlanListAdapter
                }

            } else {
                binding.recyclerTrainPlan.adapter = null
                binding.layoutEmpty.visibility = View.VISIBLE

            }
        })
    }



}